/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.stepapp.persistence;

import org.me.stepapp.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.security.Timestamp;
import java.util.Date;

/**
 *
 * @author Amber
 */
public class InsertLocation extends Activity {
    Cursor cursor;
    LocationDBAdapter db = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // GUI initialization code 
        setContentView(R.layout.insert_location);

        db = new LocationDBAdapter(this.getApplicationContext());
        db.open();

        Button btnInput = (Button) findViewById(R.id.insert_add);
        btnInput.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                EditText et_timestamp = (EditText)findViewById(R.id.insert_timestamp);
                EditText et_lat = (EditText)findViewById(R.id.insert_latitude);
                EditText et_long = (EditText)findViewById(R.id.insert_longitude);

                Date now = new Date();
                Long time = now.getTime();
                String ts = time.toString();
                et_timestamp.setText(ts);
                String timestamp = et_timestamp.getText().toString();


                String latitude = et_lat.getText().toString();
                String longitude = et_long.getText().toString();
                insertLocation(timestamp, longitude, latitude);
            }
        });
    }


    /*  Add a location based on location */
    public void insertLocation(Location x) {
        db.open();
        long id;
        id =  db.insertLocation(x);
    }

     /* Insert location using timestamp, latitude, and longitude */
    public void insertLocation(String timestamp, String latitude, String longitude) {
        db.open();
        long id;
        id = db.insertLocation(timestamp, longitude, latitude);
        String msg = "add " + timestamp;
        sendBroadCast(msg);
    }

    /* Send a broadcast message to the receiver */
    void sendBroadCast(String str)  {
        Intent intent = new Intent();
        intent.setAction("org.me.stepapp.LocationChangeReceiver");
        intent.putExtra("sender","insert_location");
        intent.putExtra("message", str );
        sendBroadcast(intent);
      }

}

